package com.deyou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.deyou.R;
import com.deyou.base.BaseActivity;
import com.deyou.util.DateUtil;
import com.deyou.util.Logger;
import com.deyou.widget.DateRangeDecorator;
import com.squareup.timessquare.CalendarCellDecorator;
import com.squareup.timessquare.CalendarPickerView;
import com.squareup.timessquare.DefaultDayViewAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectHotelDuaActivity extends BaseActivity {

    @BindView(R.id.iv_left_back)
    ImageView ivLeftBack;
    @BindView(R.id.tv_mid_title)
    TextView tvMidTitle;
    @BindView(R.id.tv_checkin_date)
    TextView tvCheckinDate;
    @BindView(R.id.tv_checkout_date)
    TextView tvCheckoutDate;
    @BindView(R.id.calender_view)
    CalendarPickerView calenderView;

    private String checkInDate, checkOutDate;
    private List<Date> selectedDateList = new ArrayList<>();
    private int selectedCount = 0;
    private int selectedType = 0; // 1-选择入住日期进入  2-选择退房日期进入
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_hotel_dua);
        ButterKnife.bind(this);
        initData();
        initView();
        selectCalender();
    }

    private void initData(){
        checkInDate = getIntent().getExtras().getString("check_in_date");
        checkOutDate = getIntent().getExtras().getString("check_out_date");
        selectedType = getIntent().getExtras().getInt("selectedType");

        if (TextUtils.isEmpty(checkInDate))
            checkInDate = DateUtil.getToday("yyyy-MM-dd");
        if (TextUtils.isEmpty(checkOutDate))
            checkInDate = DateUtil.getTomorry("yyyy-MM-dd");

        if (1 == selectedType){
            tvCheckinDate.setBackgroundColor(getResources().getColor(R.color.color_FF832D));
            tvCheckinDate.setTextColor(getResources().getColor(R.color.white));

            tvCheckoutDate.setBackgroundColor(getResources().getColor(R.color.color_F3F2F2));
            tvCheckoutDate.setTextColor(getResources().getColor(R.color.color_959090));
        } else {
            tvCheckinDate.setBackgroundColor(getResources().getColor(R.color.color_F3F2F2));
            tvCheckinDate.setTextColor(getResources().getColor(R.color.color_959090));

            tvCheckoutDate.setBackgroundColor(getResources().getColor(R.color.color_FF832D));
            tvCheckoutDate.setTextColor(getResources().getColor(R.color.white));
        }
    }

    private void initView(){
        tvMidTitle.setText("选择日期");

        tvCheckinDate.setText(checkInDate + "入住");
        tvCheckoutDate.setText(checkOutDate + "退房");

        selectedDateList.add(DateUtil.string2Date10(checkInDate));
        selectedDateList.add(DateUtil.string2Date10(checkOutDate));
    }

    private void selectCalender(){
        Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.YEAR, 1);

        calenderView.setCustomDayView(new DefaultDayViewAdapter());
        calenderView.setDecorators(Arrays.<CalendarCellDecorator>asList(new DateRangeDecorator(this)));
        calenderView.init(new Date(), nextYear.getTime()) //
                .inMode(CalendarPickerView.SelectionMode.RANGE)
                .withSelectedDates(selectedDateList);

        calenderView.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Date date) {
                selectedCount += 1;
                if (selectedCount == 1){
                    tvCheckinDate.setText(DateUtil.date2String10(date) + "入住");

                    tvCheckinDate.setBackgroundColor(getResources().getColor(R.color.color_F3F2F2));
                    tvCheckinDate.setTextColor(getResources().getColor(R.color.color_959090));

                    tvCheckoutDate.setBackgroundColor(getResources().getColor(R.color.color_FF832D));
                    tvCheckoutDate.setTextColor(getResources().getColor(R.color.white));
                    tvCheckoutDate.setText("选择退房日期");

                    checkInDate = DateUtil.date2String10(date);

                } else {
                    tvCheckoutDate.setText(DateUtil.date2String10(date) + "退房");

                    checkOutDate = DateUtil.date2String10(date);

                    Intent intent = new Intent();
                    intent.putExtra("check_in", checkInDate);
                    intent.putExtra("check_out", checkOutDate);
                    setResult(RESULT_OK, intent);
                    finish();
//                    tvCheckoutDate.setBackgroundColor(getResources().getColor(R.color.color_F3F2F2));
//                    tvCheckoutDate.setTextColor(getResources().getColor(R.color.color_959090));
//
//                    tvCheckinDate.setBackgroundColor(getResources().getColor(R.color.color_FF832D));
//                    tvCheckinDate.setTextColor(getResources().getColor(R.color.white));
                }
            }

            @Override
            public void onDateUnselected(Date date) {
                selectedCount = 0;
                Logger.i(Logger.LOGTAG + "取消选择日期", DateUtil.date2String10(date));
            }
        });
    }

    @OnClick(R.id.iv_left_back)
    public void onViewClicked() {
        finish();
    }
}
